/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aptech.eproject.controler;

import CustomeControl.CustomePanel;
import aptech.eproject.DBConnection;
import aptech.eproject.entity.Bills;
import aptech.eproject.entity.Items;
import java.awt.Color;
import java.awt.Dimension;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JPanel;

/**
 *
 * @author Loveless
 */
public class ItemControler {
    DBConnection db;
    Connection conn;
    public ItemControler() throws Exception{
        
        
    }
    public List<Items> getListItem() throws Exception{
            conn = DBConnection.getConnect();
        List<Items> listItem = new ArrayList<Items>(); 
            Statement s = conn.createStatement();
            ResultSet rs = s.executeQuery("Select * from Items");
            while(rs.next()){
                Items item = new Items();
                item.setItemID(rs.getInt("ItemID"));
                item.setItemName(rs.getString("ItemName"));
                item.setItemTypeID(rs.getInt("ItemTypeID"));
                item.setPrice(rs.getFloat("Price"));
                item.setQuantity(rs.getInt("Quantity"));
                item.setItemImg(rs.getString("ItemImg"));
                item.setItemStatus(rs.getInt("ItemStatus"));
                listItem.add(item);
            }
        conn.close();
        return listItem;
    }
    
    public boolean insertItem(Items i) {
        try {
            Date d = new Date(System.currentTimeMillis());
            conn = DBConnection.getConnect();
            PreparedStatement statement = conn.prepareStatement("INSERT INTO Items VALUES (?,?,?,?,?,?)", ResultSet.CONCUR_UPDATABLE, ResultSet.TYPE_SCROLL_SENSITIVE);
            statement.setString(1, i.getItemName());
            statement.setInt(2, i.getItemTypeID());
            statement.setFloat(3, i.getPrice());
            statement.setInt(4, i.getQuantity());
            statement.setString(5, i.getItemImg());
            statement.setInt(6, i.getItemStatus());

            statement.executeUpdate();
            return true;
        } catch (Exception ex) {
            return false;
        }
    }

    public boolean deleteBills(Bills b) {
        try {
            PreparedStatement statement = conn.prepareStatement("DELETE BillDetails WHERE BillDetailID=?", ResultSet.CONCUR_UPDATABLE, ResultSet.TYPE_SCROLL_SENSITIVE);
            statement.setInt(1, b.getBillID());
            statement.executeUpdate();
            return true;
        } catch (SQLException ex) {
            return false;
        }

    }

    public boolean updateBills(Bills b) {
        try {
             Date d = new Date(System.currentTimeMillis());
            PreparedStatement statement = conn.prepareStatement("UPDATE Bills SET "
                    + "(BillDate=?,DeptID=?,EmployeeID=?,CouponID=?,BillStatus=?)  WHERE BillID=?");
            statement.setDate(1,d);
            statement.setInt(2, b.getDeptID());
            statement.setInt(3, b.getEmployeeID());
            statement.setInt(4, b.getCouponID());
            statement.setInt(5, b.getBillStatus());
            statement.setInt(6, b.getBillID());
            statement.executeUpdate();
            return true;
        } catch (SQLException ex) {
            return false;
        }

    }
    
    
}
